package com.cloud.controller;

import java.io.File;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import javax.imageio.stream.FileImageOutputStream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.util.IdGenerator;
import org.springframework.util.MultiValueMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;
import org.springframework.web.multipart.MultipartRequest;

import com.cloud.params.PhoneParams;
import com.cloud.params.RecordResult;
import com.cloud.pojo.PhoneRelation;
import com.cloud.pojo.Salesman;
import com.cloud.result.DataResult;
import com.cloud.service.PhoneService;

/**
 *@Author: liu
 *@Date: Created on 2017年10月24日 下午2:21:39
 */
@Controller
@RequestMapping(value = "phone")
public class PhoneController {
	
	@Autowired
	private PhoneService phoneService;
	
	@RequestMapping("list")
	@ResponseBody
	public DataResult<PhoneRelation> selectAdminList(HttpServletRequest request,@RequestParam(value="page",defaultValue="0") Integer page,
			@RequestParam(value="rows",defaultValue="30") Integer rows){
		String username = (String) request.getSession().getAttribute("username");
		/*Integer id = (Integer) request.getSession().getAttribute("id");
		Salesman salesman = (Salesman) request.getSession().getAttribute("salesman");*/
		DataResult<PhoneRelation> dataResult = phoneService.selectPhoneList(page,rows,username);
		
		return dataResult;
	}
	
	@RequestMapping("selectById")
	@ResponseBody
	public PhoneRelation selectById(Integer id){
		
		PhoneRelation phoneRelation = phoneService.selectById(id);
		return phoneRelation;
	}
	
	@RequestMapping("updateRemark")
	@ResponseBody
	public Map<String, Object> updateRemark(PhoneRelation phoneRelation){
		Map<String, Object> data = new HashMap<>();
		Integer reslut = phoneService.updateRemark(phoneRelation);
		if(reslut > 0){
			data.put("status", "000");
			data.put("msg", "备注成功!");
			data.put("data", null);
		}else {
			data.put("status", "001");
			data.put("msg", "备注异常!");
			data.put("data", null);
		}
		return data;
	}
	/**
	 * @param phoneRelation
	 * @return
	 */
	@RequestMapping("updateRecordFile")
	@ResponseBody
	public Map<String, Object> updateRecordFile(PhoneParams phoneParams){
		Map<String, Object> data = new HashMap<>();
		Integer reslut = phoneService.updateRecordFile(phoneParams);
		if(reslut > 0){
			data.put("status", "000");
			data.put("msg", "保存录音文件成功!");
			data.put("data", null);
		}else if(reslut < 0){
			data.put("status", "001");
			data.put("msg", "保存录音文件异常!");
			data.put("data", null);
		}else{
				data.put("status", "002");
				data.put("msg", "该电话数据库中没有!");
				data.put("data", null);
		}
		return data;
	}
	
	@RequestMapping("upload")
	@ResponseBody
	public Map<String, Object> upload(MultipartFile file,String type,HttpServletRequest request,HttpServletResponse response){
		response.setContentType("text/html; charset=utf-8");
		Map<String, Object> data = new HashMap<String, Object>();
		try {
			String filename = file.getOriginalFilename();
			byte[] bytes = file.getBytes();
			String path = "E:"+File.separator+"record";
			
            File binaryFile = new File(path);
            if(!binaryFile.exists()){
                binaryFile.mkdir();
            }
            String imgPath = path+File.separator+filename;
            File f = new File(imgPath);
            FileImageOutputStream imageOutput = new FileImageOutputStream(f);
            imageOutput.write(bytes, 0, bytes.length);
            imageOutput.close();
//            String url = "http://192.168.1.162:8081"+File.separator+filename;
            //String url = "http://118.190.13.47:8081"+File.separator+filename;
			data.put("status", "000");
			data.put("msg", "上传成功");
			data.put("data", "");
			//data.put("url", url);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return data;
	}
	
	/**
	 * 条件查询录音列表
	 * @param page
	 * @param rows
	 * @param params
	 * @return
	 */
	@RequestMapping("record_list")
	@ResponseBody
	public DataResult<RecordResult> selectRecordList(@RequestParam(value="page",defaultValue="0") Integer page,
			@RequestParam(value="rows",defaultValue="30") Integer rows,PhoneParams params){
		
		DataResult<RecordResult> dataResult = phoneService.selectRecordList(page,rows,params);
		
		return dataResult;
	}
}
